﻿using System;
using System.Text.RegularExpressions;

namespace SimpleMediaManager
{
    public class Format
    {
        private static string[] filenameRegex = {
            @"^(?<title>.*?)\.?(?<year>\d{4})+\.?(?<tags>.*?)\.?(?<source>BDRip|BRRip|BluRay|HDDVD|HDRip|DVDRip|R5|DVDScr|WORKPRiNT|WP|TC|TS|CAM|VHS|PRVHS|HDTV|PDTV)?\.?(480p|720i|720p|1080i|1080p|m720p|m1080p)?\.?(?<sound>AC3)?\.?(?<encoding>XviD|DivX|x264)\.?(?<sound>AC3)?-(?<group>[\w-]+)\.?(CD(?<disc>[1-9]))?$",
            @"^(?<title>.*?)\.(?<tags>[iA-Z]+?)\.?(?<source>BDRip|BRRip|BluRay|HDDVD|HDRip|DVDRip|R5|DVDScr|WORKPRiNT|WP|TC|TS|CAM|VHS|PRVHS|HDTV|PDTV)?\.?(480p|720i|720p|1080i|1080p|m720p|m1080p)?\.?(?<sound>AC3)?\.?(?<encoding>XviD|DivX|x264)\.?(?<sound>AC3)?-(?<group>[\w-]+)\.?(CD(?<disc>[1-9]))?$",
            @"^(?<title>.*?)\[(?<year>\d{4})\](?<source>BDRip|BRRip|BluRay|HDDVD|HDRip|DVDRip|R5|DVDScr|WORKPRiNT|WP|TC|TS|CAM|VHS|PRVHS|HDTV|PDTV)\[.*\]-(?<group>[\w-]+)\.?(CD(?<disc>[1-9]))?$",
            @"^(?<title>.*?)( )?(\((?<year>\d{4})\)|(?<year>\d{4}))?( )?(-)?( )*(?<source>BDRip|BRRip|BluRay|HDDVD|HDRip|DVDRip|R5|DVDScr|WORKPRiNT|WP|TC|TS|CAM|VHS|PRVHS|HDTV|PDTV)?( x264)?( )?(480p|720i|720p|1080i|1080p|m720p|m1080p)?( | - |-)?(?<encoding>XviD|DivX|x264)?( | - |-)?(?<group>[\w-]+)?( )?(- (Disk|Disc|CD)( )?(?<disc>[1-9]))?$"
        };

        private static string RemoveDots(string input)
        {
            input = input.Trim('.').Replace('.', ' ');
            return input;
        }

        public static Movie GetFileInfo(string filename)
        {
            Movie movie = new Movie();
            filename = filename.Trim();
            Match match = Regex.Match(filename, filenameRegex[0], RegexOptions.IgnoreCase);

            int i = 1;
            while (!match.Success && i < filenameRegex.Length)
            {
                match = Regex.Match(filename, filenameRegex[i], RegexOptions.IgnoreCase);
                i += 1;
            }

            if (match.Success)
            {
                if (match.Groups["title"].Captures.Count == 1)
                {
                    movie.FileTitle = RemoveDots(match.Groups["title"].Captures[0].Value);
                }

                if (match.Groups["year"].Captures.Count == 1)
                {
                    movie.FileYear = RemoveDots(match.Groups["year"].Captures[0].Value);
                }

                if (match.Groups["tags"].Captures.Count > 0)
                {
                    if (match.Groups["tags"].Captures[0].Value != "")
                    {
                        movie.FileTags = match.Groups["tags"].Captures[0].Value.Split('.');
                    }
                }

                if (match.Groups["source"].Captures.Count > 0)
                {
                    string quality = RemoveDots(match.Groups["source"].Captures[0].Value);

                    foreach (Source source in Enum.GetValues(typeof(Source)))
                    {
                        if (quality.ToLower() == source.ToString().ToLower())
                        {
                            movie.FileSource = source;
                        }
                    }
                }

                if (match.Groups["sound"].Captures.Count > 0)
                {
                    if (RemoveDots(match.Groups["source"].Captures[0].Value).ToUpper() == "AC3")
                    {
                        movie.FileAc3 = true;
                    }
                }

                if (match.Groups["encoding"].Captures.Count > 0)
                {
                    string encoding = RemoveDots(match.Groups["encoding"].Captures[0].Value);
                    foreach (Encoding enc in Enum.GetValues(typeof(Encoding)))
                    {
                        if (encoding.ToLower() == enc.ToString().ToLower())
                        {
                            movie.FileEncoding = enc;
                        }
                    }
                }

                if (match.Groups["sound"].Captures.Count > 0)
                {
                    string sound = match.Groups["sound"].Captures[0].Value;
                    if (RemoveDots(match.Groups["sound"].Captures[0].Value.ToUpper()) == "AC3")
                    {
                        movie.FileAc3 = true;
                    }
                }

                if (match.Groups["group"].Captures.Count == 1)
                {
                    movie.FileGroup = RemoveDots(match.Groups["group"].Captures[0].Value);
                }

                movie.FileDiscs = 1;
            }
            else
            {

            }

            return movie;
        }
    }
}
